$(document).ready(function () {
	sendAjax(1,1,'');//排行榜信息
	$('#cancel_btn').click(function(){
		$('#searchname').val('');
		window.location.href = gotoactive;
	})
	var swiper = new Swiper('.swiper-container', {
		width: 0,
        height: 0,
        slidesPerView: 3,
        centeredSlides: true,
        paginationClickable: true,
        passiveListeners : false,
        spaceBetween: 0,
        grabCursor: true,
        slidesOffsetBefore : 0, //与左边框偏移量
        direction : 'horizontal',
        resistanceRatio: 0,
        centeredSlides: true,
        paginationClickable: true,
        onInit: function(swiper){ //初始化改变宽度
	    	var w = 3.5+'rem';
			$(".swiper-slide").css('width', w);
	    },
	    onTouchEnd: function(swiper){
	    	var x = swiper.getWrapperTranslate('x');
	    	if(x <= -320) {
                swiper.setWrapperTranslate(-320,0,0);
            }
            if(x >= 645) {
                swiper.setWrapperTranslate(645,0,0);
            }
	    }
   });
})
//距离本周结束时间倒计时
function daojishi() {
    clearInterval(timer);
    var nowtime = Date.parse(new Date());
    if((starttime*1000) > nowtime){
        clearInterval(timer);
        $('.countdown').html('本周比赛未开始，敬请期待！');
    }else if((endtime*1000) < nowtime){
        clearInterval(timer);
        $('.countdown').html('本周比赛已结束');
    }else{
        var leftTime = (endtime * 1000 - nowtime) / 1000;
        var day = Math.floor(leftTime / 60 / 60 / 24); //计算剩余的天数
        var hours = Math.floor(leftTime / 60 / 60 % 24); //计算剩余的小时
        var minutes = Math.floor(leftTime / 60 % 60);//计算剩余的分钟
        var seconds = Math.floor(leftTime % 60);//计算剩余的秒数
        msg = "<span>" + day + "</span>" + "<span>天</span>" + "<span>" + hours + "</span>" + "<span>小时</span>" + "<span>" + minutes + "</span>" + "<span>分</span>" + "<span>" + seconds + "</span>" + "<span>秒</span>";
        $('.countdown').html("距离本周结束:" + msg);
        timer = setInterval("daojishi()", 1000);
    }
}
//分页
function sendAjax(page,type,searchname){
    var url = gamerank;
    $.ajax({  
        url: url,  
        type:'GET',  
        cache:false,   
        dataType: 'json',
        data : {
            searchname: searchname,
            type: type,
            page:page,
            typeform: 1
        },
        success: function(json) {
            if(json){
                var gameSTR = '';
                gameSTR+='<ul>';
                $.each(json.dataarr,function(key,val){
                    var dataarr = json.dataarr[key];
                    gameSTR+='<li onclick="detail('+(((json.page)-1)*10+Number(key)+1)+','+dataarr.id+')">';
                    gameSTR+='<div>'+(((json.page)-1)*10+Number(key)+1)+'</div>';//比赛排名
                    gameSTR+='<div>'+dataarr.username+'</div>';
                    gameSTR+='<div>'+dataarr.ordernum+'</div>'; //周订阅数量
                    gameSTR+='<div>'+Number(dataarr.winrate).toFixed(2)+'%</div>'; //胜率
                    gameSTR+='<div>'+Number(dataarr.week_final_point).toFixed(2)+'</div>'; //周盈利
                    // gameSTR+='<div>'+Number(dataarr.total_final_point).toFixed(2)+'</div>'; //总盈利
                    gameSTR+='</li>';
                });
			    gameSTR+='</ul>';
			    if(json.allnum != 0){
			        gameSTR+='<div id="pagination">';
			        	if(json.page == 1 || json.page == 0) {
			        		gameSTR+='<span id="pre" class="home" style="background-color:#9EA4A9;color:#fff;">上一页</span>';
			        	}else {
			        		gameSTR+='<span id="pre" class="home" onclick="gotopage('+json.page+',0)" style="background-color:#F73353;color:#fff;">上一页</span>';
			        	}
						gameSTR+='<span class="page">'+json.page+'</span>';
						if(json.page == json.allpage){
							gameSTR+='<span id="next" class="end" style="background-color:#9EA4A9;color:#fff;">下一页</span>';
						}else{
							gameSTR+='<span id="next" class="end" onclick="gotopage('+json.page+',1)" style="background-color:#F73353;color:#fff;">下一页</span>';
						}
			        gameSTR+='</div>';
				}
			    $('.list_main').html(gameSTR);
            }
        }
    });
}
function gotopage(page,pagetype,searchname){
	// $('#searchname').css('color','#30353c');
    var searchname = $('#searchname').val();
    if(pagetype == 0) {
        sendAjax(page,0,searchname);
    }else{
    	sendAjax(page+1,1,searchname);
    }
}

// 周榜页
function showWeekRank(start,end){
	window.location.href = weekrank+'?start='+start+"&end="+end;
}
//比赛排行详情页
function detail(ranking,userid) {
	window.location.href = rankdetail+"?ranking="+ranking+"&userid="+userid;
}
